package b2.widget.fort
{
	import b2.config.*;
	import b2.net.*;
	import b2.player.*;
	import b2.scene.*;
	import b2.scene.fight.*;
	import b2.scene.guide.*;
	import b2.util.*;
	import b2.widget.*;
	import b2.widget.tab.*;
	import b2.widget.warehouse.*;
	
	import flash.display.Sprite;
	import flash.events.*;
	import flash.filters.*;
	import flash.text.*;
	import flash.utils.Timer;

	public class MatchDetailTab extends Tab
	{
		
		private var fortressDialog:FortressDialog;
		private var titleText:TextField;
		private var startTimeText:TextField;
		private var roundText:TextField;
		private var fpLimitText:TextField;
		private var lvLimitText:TextField;
		private var countDownText:TextField;
		//时钟文字
		private var clockText:TextField;
		//每轮战役介绍
		private var roundCampText:TextField;
		//赛制介绍
		private var matchIntroText:TextField;
		private var descripText:TextField;
		
		private var container:Sprite;
		//时钟
		private var timer:Timer;
		//当前比赛索引
		public var matchIndex:int;
		//报名按钮
		private var signUpButton:BasicButton;
		
		public function MatchDetailTab(fortressDialog:FortressDialog)
		{
			super("战役详情", 2, 2);
			setPageSelectorPos(200, 400);
			this.fortressDialog = fortressDialog;
			//线框
			var lineRect:Sprite = new Sprite();
			lineRect.graphics.lineStyle(2, 0x27AAE1);
			lineRect.graphics.drawRoundRect(4, 39, 346, 116, 20, 20);
			lineRect.graphics.drawRoundRect(4, 155, 346, 108, 20, 20);
			lineRect.graphics.drawRoundRect(4, 263, 346, 180, 20, 20);
			lineRect.filters = [new DropShadowFilter(2, 45, 0x000000, 1, 2, 2, 1, 1, false)];
			addChild(lineRect);
			//线框色块
			var rrect:Sprite = new Sprite();
			rrect.graphics.beginFill(0x27AAE1);
			rrect.graphics.drawRoundRect(6, 41, 342, 25, 15, 15);
			rrect.graphics.drawRoundRect(6, 157, 342, 25, 15, 15);
			rrect.graphics.drawRoundRect(6, 265, 342, 25, 15, 15);
			rrect.graphics.endFill();
			addChild(rrect);
			//战役名字
			titleText = new TextField();
			titleText.selectable = false;
        	titleText.autoSize = TextFieldAutoSize.CENTER;
			titleText.defaultTextFormat = new TextFormat("微软雅黑", 15, 0xFFFF00);
			titleText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			titleText.x = 180;
			titleText.y = 38;
			addChild(titleText);
			//报名时间
			var startTimeLable:TextField = new TextField();
			startTimeLable.selectable = false;
        	startTimeLable.autoSize = TextFieldAutoSize.LEFT;
			startTimeLable.defaultTextFormat = new TextFormat("微软雅黑", 12, 0xFFFF00);
			startTimeLable.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			startTimeLable.x = 15;
			startTimeLable.y = 70;
			startTimeLable.text = "开始时间";
			addChild(startTimeLable);
			//报名时间
			startTimeText = new TextField();
			startTimeText.selectable = false;
        	startTimeText.autoSize = TextFieldAutoSize.LEFT;
			startTimeText.defaultTextFormat = new TextFormat("微软雅黑", 25, 0xFFFFFF, true);
			startTimeText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			startTimeText.x = 78;
			startTimeText.y = 70;
			//轮数
			roundText = new TextField();
			roundText.selectable = false;
        	roundText.autoSize = TextFieldAutoSize.LEFT;
			roundText.defaultTextFormat = new TextFormat("微软雅黑", 12, 0xFFFFFF);
			roundText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			roundText.x = 15;
			roundText.y = 90;
			//战力限制
			fpLimitText = new TextField();
			fpLimitText.selectable = false;
        	fpLimitText.autoSize = TextFieldAutoSize.LEFT;
			fpLimitText.defaultTextFormat = new TextFormat("微软雅黑", 12, 0xFFFFFF);
			fpLimitText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			fpLimitText.x = 15;
			fpLimitText.y = 110;
			//等级限制
			lvLimitText = new TextField();
			lvLimitText.selectable = false;
        	lvLimitText.autoSize = TextFieldAutoSize.LEFT;
			lvLimitText.defaultTextFormat = new TextFormat("微软雅黑", 12, 0xFFFFFF);
			lvLimitText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			lvLimitText.x = 15;
			lvLimitText.y = 130;
			//倒计时文字
			countDownText = new TextField();
			countDownText.selectable = false;
        	countDownText.autoSize = TextFieldAutoSize.LEFT;
			countDownText.defaultTextFormat = new TextFormat("微软雅黑", 12, 0xFFFF00);
			countDownText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			countDownText.x = 180;
			countDownText.y = 70;
			//时钟文字
			clockText = new TextField();
			clockText.selectable = false;
        	clockText.autoSize = TextFieldAutoSize.CENTER;
			clockText.defaultTextFormat = new TextFormat("微软雅黑", 48, 0xFFFFFF, true);
			clockText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			clockText.y = 85;
			//敌方信息
			roundCampText = new TextField();
			roundCampText.selectable = false;
        	roundCampText.autoSize = TextFieldAutoSize.LEFT;
			roundCampText.defaultTextFormat = new TextFormat("微软雅黑", 15, 0xFFFF00);
			roundCampText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			roundCampText.text = "敌军信息";
			roundCampText.x = 150;
			roundCampText.y = 155;
			//赛制介绍
			matchIntroText = new TextField();
			matchIntroText.selectable = false;
        	matchIntroText.autoSize = TextFieldAutoSize.CENTER;
			matchIntroText.defaultTextFormat = new TextFormat("微软雅黑", 15, 0xFFFF00);
			matchIntroText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			matchIntroText.text = "赛制介绍";
			matchIntroText.x = 150;
			matchIntroText.y = 265;
			//赛制介绍
			descripText = new TextField();
			descripText.selectable = false;
        	descripText.autoSize = TextFieldAutoSize.LEFT;
			descripText.defaultTextFormat = new TextFormat("微软雅黑", 13, 0xFFFFFF);
			descripText.multiline = true;
			descripText.wordWrap = true;
			descripText.width = 320;
			descripText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			descripText.x = 15;
			descripText.y = 292;
			
			var LeftArrowButton:Class = ResourceHelper.getInstance().getClassByName("TabPagePageUp");
			var RightArrowButton:Class = ResourceHelper.getInstance().getClassByName("TabPagePageDown");
			//左箭头
			var leftArrow:BasicButton = new BasicButton(LeftArrowButton);
			leftArrow.x = 17;
			leftArrow.y = 412;
			leftArrow.addEventListener(MouseEvent.CLICK, onLeftArrowMouseClick);
			addChild(leftArrow);
			//左箭头文字
			var leftArrowText:TextField = new TextField();
			leftArrowText.selectable = false;
        	leftArrowText.autoSize = TextFieldAutoSize.LEFT;
			leftArrowText.defaultTextFormat = new TextFormat("微软雅黑", 12, 0x000000, true);
			leftArrowText.text = "上一场";
			leftArrowText.x = leftArrow.x + 26;
			leftArrowText.y = leftArrow.y;
			addChild(leftArrowText);
			//右箭头
			var rightArrow:BasicButton = new BasicButton(RightArrowButton);
			rightArrow.x = 317;
			rightArrow.y = 412;
			rightArrow.addEventListener(MouseEvent.CLICK, onRightArrowMouseClick);
			addChild(rightArrow);
			//右箭头文字
			var rightArrowText:TextField = new TextField();
			rightArrowText.selectable = false;
        	rightArrowText.autoSize = TextFieldAutoSize.LEFT;
			rightArrowText.defaultTextFormat = new TextFormat("微软雅黑", 12, 0x000000, true);
			rightArrowText.text = "下一场";
			rightArrowText.x = rightArrow.x - 45;
			rightArrowText.y = rightArrow.y;
			addChild(rightArrowText);
			
			setPagePos(15, 297);
//			setPageSelectorPos(210, 412);
			hidePageSelector();
			this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			this.addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
		}
		
		private function onLeftArrowMouseClick(event:MouseEvent):void
		{
			var campIndex:int = fortressDialog.currentSelectedCampItem.campIndex;
			var campConfig:CampConfig = Config.campConfigs[campIndex];
			var currentMatch:int = campConfig.matchIndex.indexOf(matchIndex.toString());
			if (currentMatch >= 0)
				currentMatch = (currentMatch + campConfig.matchIndex.length - 1) % campConfig.matchIndex.length;
			else
				currentMatch = 0;
			refreshMatch(campConfig.matchIndex[currentMatch]);
		}
		
		private function onRightArrowMouseClick(event:MouseEvent):void
		{
			var campIndex:int = fortressDialog.currentSelectedCampItem.campIndex;
			var campConfig:CampConfig = Config.campConfigs[campIndex];
			var currentMatch:int = campConfig.matchIndex.indexOf(matchIndex.toString());
			if (currentMatch >= 0)
				currentMatch = (currentMatch + 1) % campConfig.matchIndex.length;
			else
				currentMatch = 0;
			refreshMatch(campConfig.matchIndex[currentMatch]);
		}
		
		private function onAddedToStage(event:Event):void
		{
			timer = new Timer(1000);
			timer.addEventListener(TimerEvent.TIMER, onTick);
			timer.start();
		}

		private function onRemovedFromStage(event:Event):void
		{
			timer.stop();
			timer.removeEventListener(TimerEvent.TIMER, onTick);
			timer = null;
		}
		
		//时钟跳动
		private function onTick(event:TimerEvent):void
		{
			var matchConfig:MatchConfig = Config.matchConfigs[matchIndex];
			var currentSeconds:int = int(ServerHelper.getInstance().currentSeconds()) % (24 * 60 * 60);
			var remainSeconds:int; 
			if (currentSeconds < matchConfig.startTime * 60)
			{
				countDownText.text = "距比赛开始";
				remainSeconds = matchConfig.startTime * 60 - currentSeconds;
				signUpButton.disable = true;
			}
			else if (currentSeconds < (matchConfig.startTime + matchConfig.duration) * 60)
			{
				countDownText.text = "据比赛结束";
				remainSeconds = (matchConfig.startTime + matchConfig.duration) * 60 - currentSeconds;
				signUpButton.disable = false;
			}
			else
			{
				countDownText.text = "比赛已经结束";
				remainSeconds = 0;
				signUpButton.disable = true;
			}
			var minutes:int = remainSeconds / 60;
			var seconds:int = remainSeconds % 60;
			var timeStr:String = (minutes < 10 ? "0" : "") + minutes + ":" + (seconds < 10 ? "0" : "") + seconds;
			clockText.text = " " + timeStr + " ";
			clockText.x = 250 - clockText.width / 2;
		}
		
		//刷新比赛信息
		private function refreshMatch(matchIndex:int):void
		{
			if (container != null && container.parent != null)
			{
				container.parent.removeChild(container);
			}
			container = new Sprite();
			this.matchIndex = matchIndex;
			var matchConfig:MatchConfig = Config.matchConfigs[matchIndex];
			var matchCampConfig:CampConfig = Config.campConfigs[matchConfig.campIndex];
			//背景水纹
			var enemies:Array = matchCampConfig.getEnemy();
			var enemyConfig:EnemyConfig = Config.enemyConfigs[enemies[0][0]];
			var tankConfig:TankConfig = Config.objectConfigs[enemyConfig.tankIndex];
			var Bgr:Class = ResourceHelper.getInstance().getClassByName(tankConfig.iconImage);
			var bgr:Sprite = new Bgr();
			bgr.x = 200;
			bgr.y = 150;
			bgr.width = 350;
			bgr.height = 350;
			bgr.filters = [new ColorMatrixFilter(BFont.blackWhiteFilter)];
			bgr.alpha = 0.2;
			fortressDialog.changeBgr(bgr);
			//立即报名
			var ButtonBgr0:Class = ResourceHelper.getInstance().getClassByName("ButtonBgr0");
			signUpButton = new BasicButton(ButtonBgr0, "立即参加", 12, 70, 25);
			signUpButton.x = 150;
			signUpButton.y = 410;
			signUpButton.addEventListener(MouseEvent.CLICK, onSignUpButtonClick);
			container.addChild(signUpButton);
			//文字信息
			titleText.text = matchCampConfig.name;
			var honours:int = matchConfig.startTime / 60;
			var minutes:int = matchConfig.startTime % 60;
			var timeStr:String = (honours < 10 ? "0" : "") + honours + ":" + (minutes < 10 ? "0" : "") + minutes;
			startTimeText.htmlText = timeStr;
			fpLimitText.htmlText = "<font color='#FFFF00'>战力限制</font>    " + matchConfig.fpLimitLow + "-" + matchConfig.fpLimitUp;
			lvLimitText.htmlText = "<font color='#FFFF00'>等级限制</font>    " + matchConfig.levelLimitLow + "-" + matchConfig.levelLimitUp;
			//敌人信息
			for (var i:int = 0; i < Math.min(enemies.length, 4); i++)
			{
				var enemyItem:EnemyTankItem;
				var enemyLevel:int = matchConfig.difficulty;
				var enemyBossLevel:int = enemies[i][2] > 0 ? 2 : 0; 
				enemyItem = new EnemyTankItem(enemies[i][0], enemyLevel, enemyBossLevel);
				enemyItem.x = 18 + i * 82;
				enemyItem.y = 187;
				container.addChild(enemyItem);
			}
			descripText.htmlText = matchCampConfig.description;
			container.addChild(startTimeText);
			container.addChild(roundText);
			container.addChild(fpLimitText);
			container.addChild(lvLimitText);
			container.addChild(countDownText);
			container.addChild(clockText);
			container.addChild(roundCampText);
			container.addChild(matchIntroText);
			container.addChild(descripText);
			addChild(container);
			onTick(null);
		}
		
		public function refresh():void
		{
			var campIndex:int = fortressDialog.currentSelectedCampItem.campIndex;
			var campConfig:CampConfig = Config.campConfigs[campIndex];
			//计算当前的时间
			var currentMinutes:int = int(ServerHelper.getInstance().currentSeconds() / 60) % (24 * 60);
			var currentMatch:int = 0;
			var matchConfig:MatchConfig;
			while (currentMatch < campConfig.matchIndex.length)
			{
				matchConfig = Config.matchConfigs[campConfig.matchIndex[currentMatch]];
				if (currentMinutes < matchConfig.startTime + matchConfig.duration)
					break;
				currentMatch++;
			}
			if (currentMatch >= campConfig.matchIndex.length)
				currentMatch = 0;
			refreshMatch(campConfig.matchIndex[currentMatch]);
		}
		
		private function onSignUpButtonClick(event:MouseEvent):void
		{
			var matchConfig:MatchConfig = Config.matchConfigs[matchIndex];
			var me:PlayerInfo = PlayerInfoManager.getInstance().me;
			var campTank:TankInfo = PlayerInfoManager.getInstance().tanks[me.campTankId];
			var currentSeconds:int = int(ServerHelper.getInstance().currentSeconds()) % (24 * 60 * 60);
			if (me.level < matchConfig.levelLimitLow || me.level > matchConfig.levelLimitUp)
			{
        		var popMessage:PopMessage = new PopMessage("经验等级不满足参赛要求", 0xFF0000);
	        	WidgetManager.getInstance().popOut([popMessage], false, true);
			}
			else if (campTank.fightPoint < matchConfig.fpLimitLow || campTank.fightPoint > matchConfig.fpLimitUp)
			{
        		popMessage = new PopMessage("坦克战力不满足参赛要求", 0xFF0000);
	        	WidgetManager.getInstance().popOut([popMessage], false, true);
			}
			else if (currentSeconds < matchConfig.startTime * 60 || currentSeconds > (matchConfig.startTime + matchConfig.duration) * 60)
			{
        		popMessage = new PopMessage("未到比赛时间", 0xFF0000);
	        	WidgetManager.getInstance().popOut([popMessage], false, true);
			}
			else
			{
				ServerHelper.getInstance().checkMatchQualification(matchIndex);
				ServerHelper.getInstance().addEventListener(ServerEvent.CHECK_MATCH_QUALIFICATION, onCheckMatchQualification);
			}
		}
		
		private function onCheckMatchQualification(event:ServerEvent):void
		{
			ServerHelper.getInstance().removeEventListener(ServerEvent.CHECK_MATCH_QUALIFICATION, onCheckMatchQualification);
			var matchIndex:int = parseInt(event.userData[0]);
			var qulified:Boolean = event.userData[1] as Boolean;
			if (!qulified)
			{
        		var popMessage:PopMessage = new PopMessage("今天已经上榜两次，歇歇吧(ˇˍˇ）", 0xFF0000);
	        	WidgetManager.getInstance().popOut([popMessage], false, true);
			}
			else
			{
				var matchConfig:MatchConfig = Config.matchConfigs[matchIndex];
				SceneManager.getInstance().loadScene(FightScene.getInstance());
				FightScene.getInstance().startMatch(matchConfig.campIndex, matchIndex);
			}
		}
		
	}
}